package com.aso114.project.exam;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.PracticeReportAdapter;
import com.aso114.project.base.BaseSimpleActivity;
import com.aso114.project.bean.EvenBaseBean;
import com.aso114.project.bean.QuestionBaseBean;
import com.aso114.project.commonview.MyGridView;
import com.aso114.project.mvp.activity.SatGuideActivity;
import com.aso114.project.util.Constant;
import com.aso114.project.util.Helper;
import com.aso114.project.util.SystemBarHelper;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 练习报告
 */
public class PracticeReportActivity extends BaseSimpleActivity {
    //图片编号
    @BindView(R.id.layout_title_bar_back_iv)
    ImageView layoutTitleBarBackIv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;

    @BindView(R.id.analysis)
    TextView analysis;
    @BindView(R.id.missanalysis)
    TextView missanalysis;
    PracticeReportAdapter adapter;
    @BindView(R.id.gridview)
    MyGridView gridview;
    @BindView(R.id.number)
    TextView number;
    @BindView(R.id.introduce)
    TextView introduce;
    @BindView(R.id.imghead)
    ImageView imghead;
    Intent intent;
    ArrayList<QuestionBaseBean> errorbaseBean = new ArrayList<>();
    ArrayList<QuestionBaseBean> errorbaseBeanBackUp = new ArrayList<>();
    ArrayList<QuestionBaseBean> baseBean = new ArrayList<>();
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;

    @Override
    protected int getResouseId() {
        return R.layout.practicereport_xml;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(this, 0);
        SystemBarHelper.setHeightAndPadding(this, titleTopLy);
        layoutTitleBarTitleTv.setText("练习报告");
        adapter = new PracticeReportAdapter(this, baseBean);
        gridview.setAdapter(adapter);
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                EventBus.getDefault().postSticky(new EvenBaseBean(baseBean, 1));

                Constant.title_tv = "5";

                intent = new Intent(PracticeReportActivity.this, Error_Analysis_Activity.class);
                intent.putExtra(Error_Analysis_Activity.IS_EXAM, true);
                intent.putExtra("titletype", "2");
                intent.putExtra("isAnalysis", true);
                intent.putExtra("position", position);
                intent.putExtra("isAnalysisOpen", true);
                startActivity(intent);

            }
        });

    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(ArrayList<QuestionBaseBean> event) {
        baseBean = event;
        //处理错误数据
        for (int j = 0; j < baseBean.size(); j++) {
            if (!baseBean.get(j).isChooseResult()) {
                errorbaseBeanBackUp.add(baseBean.get(j));
            }
        }

        adapter.setData(baseBean);
        adapter.notifyDataSetChanged();
        int rightAnswer = 0;

        for (QuestionBaseBean r : baseBean) {
            if (r.isChooseResult()) {
                rightAnswer++;
            }
        }

        number.setText(rightAnswer + "");

        int score = 0;

        if (baseBean != null && baseBean.size() != 0) {
            score = rightAnswer * 100 / baseBean.size();
        }

        if (score < 80) {
            introduce.setText("再接再厉");
            imghead.setImageResource(R.mipmap.ej_zjzl_ic);
        } else {
            introduce.setText("真是知识渊博");
            imghead.setImageResource(R.mipmap.ej_lihai_ic);
        }
    }

    @Override
    protected void initData() {


    }


    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            if (AnswerCardActivity.issubmitpaper == true) {//表示已经提交过试卷  要退出到列表
                if (AnswerCardActivity.instance != null) {
                    AnswerCardActivity.instance.finish();
                }
                if (QuestionTestActivity.instance != null) {
                    QuestionTestActivity.instance.finish();
                }

                if (SatGuideActivity.instance != null) {
                    SatGuideActivity.instance.finish();
                }



            }
            finish();

            return false;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void getBroadcast(Context context, Intent intent) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @OnClick({R.id.layout_title_bar_back_iv, R.id.analysis, R.id.missanalysis})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_title_bar_back_iv:
                if (AnswerCardActivity.issubmitpaper == true) {//表示已经提交过试卷  要退出到列表
                    if (AnswerCardActivity.instance != null) {
                        AnswerCardActivity.instance.finish();
                    }
                    if (QuestionTestActivity.instance != null) {
                        QuestionTestActivity.instance.finish();
                    }
                    if (SatGuideActivity.instance != null) {
                        SatGuideActivity.instance.finish();
                    }

                }
                finish();
                break;
            case R.id.analysis:
                Constant.title_tv = "5";
                EventBus.getDefault().postSticky(new EvenBaseBean(baseBean, 1));

                intent = new Intent(this, Error_Analysis_Activity.class);
                intent.putExtra(Error_Analysis_Activity.IS_EXAM, true);
                intent.putExtra("titletype", "2");
                intent.putExtra("isAnalysis", true);
                intent.putExtra("isAnalysisOpen", true);
                intent.putExtra("isReportTo", true);
                startActivity(intent);
                break;
            case R.id.missanalysis:
                Constant.title_tv = "5";

                errorbaseBean.clear();
                errorbaseBean.addAll(errorbaseBeanBackUp);
                if (errorbaseBean.size() <= 0) {
                    Helper.showToast("恭喜你，全部答对了");
                    break;
                }
                EventBus.getDefault().postSticky(new EvenBaseBean(errorbaseBean, 1));

                Intent intent = new Intent(this, Error_Analysis_Activity.class);
                intent.putExtra(QuestionTestActivity.IS_EXAM, true);
                intent.putExtra("titletype", "1");
                intent.putExtra("isAnalysis", true);
                intent.putExtra("isAnalysisOpen", true);
                intent.putExtra("isReportTo", true);
                startActivity(intent);
                break;
        }
    }
}

